package com.xiao.community.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiao.community.entity.*;
import com.xiao.community.entity.vo.Po;
import com.xiao.community.pay.PayController;
import com.xiao.community.service.*;
import com.xiao.community.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xiao.group.common.utils.PageUtils;
import com.xiao.group.common.utils.R;

import javax.servlet.http.HttpServletResponse;


/**
 * 
 *
 * @author 韩国涛
 * @email 18862701405@gmail.com
 * @date 2021-09-10 15:22:54
 */
@RestController
@RequestMapping("community/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private PintuanOrderService pintuanOrderService;

    @Autowired
    private PintuanService pintuanService;

    @Autowired
    private QianUserService qianUserService;

    @Autowired
    private PayController payController;

    @Autowired
    private IdWorker idWorker;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 该活动拼团订单集合
     */
    @RequestMapping("/orderlist/{pintuanId}")
    public List<OrderEntity> list(@PathVariable("pintuanId") Integer pintuanId){
        return orderService.orderlist(pintuanId,1);
    }

    @RequestMapping("/ordernumlist")
    public Integer ordernumlist(@RequestBody Po po){
        Integer orderNum = orderService.selectByNum(po.getOrderId());

        Integer pintuannum=pintuanService.selectByNum(po.getPintuanId());
        if (orderNum<pintuannum){
            return 1;
        }
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderId(po.getOrderId());
        orderEntity.setIfstart(0);
        orderService.updateByOrderIfStart(orderEntity);
        return 0;
    }

    @RequestMapping("/selectOrderId/{orderId}")
    public void selectOrderId(@PathVariable("orderId") String orderId) {

        List<OrderEntity> orderEntities=orderService.selectByOrderId(orderId);
        PintuanOrderEntity entity = new PintuanOrderEntity();
        for (OrderEntity orderEntity : orderEntities) {
            entity.setGoodsId(orderEntity.getGoodsId());
            entity.setUserId(orderEntity.getUserId());
            entity.setIfzhifu(0);
            entity.setIftrue(0);
            entity.setPintuanId(orderEntity.getPintuanId());
        }
        pintuanOrderService.save(entity);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{orderId}")
    public R info(@PathVariable("orderId") Integer orderId){
		OrderEntity order = orderService.getById(orderId);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody OrderEntity order){

        if (order.getOrderId() ==""){
            //生成UUID
            String s = idWorker.nextId() + "";
            order.setOrderId(s);
            order.setIfstart(1);
            orderService.save(order);
        }else {

            order.setIfstart(0);
            orderService.save(order);
        }

        PintuanOrderEntity entity = new PintuanOrderEntity();
        entity.setGoodsId(order.getGoodsId());
        entity.setUserId(order.getUserId());
        entity.setIfzhifu(0);
        entity.setIftrue(0);
        entity.setPintuanId(order.getPintuanId());
        pintuanOrderService.save(entity);

        QianUserEntity qianUserEntity=qianUserService.selectUid(order.getUserId());
        BigDecimal userMoney = qianUserEntity.getUserMoney();
        BigDecimal goodsPrice = order.getAllmoney();
        if (userMoney.compareTo(goodsPrice)<1){
           return R.error();
        }else {
            BigDecimal usersalay=userMoney.subtract(goodsPrice);
            qianUserEntity.setUserMoney(usersalay);
            qianUserService.updateById(qianUserEntity);
            System.out.println("已生成拼团订单");
        }

        PintuanEntity goodsId = pintuanService.selectByGoodsId(order.getGoodsId());
        goodsId.setPintuanKucun(goodsId.getPintuanKucun()-order.getGoodsNum());
        pintuanService.updateById(goodsId);
        System.out.println("拼团库存已更改");

        return R.ok().put("order",order);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody OrderEntity order){
		orderService.updateById(order);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] orderIds){
		orderService.removeByIds(Arrays.asList(orderIds));

        return R.ok();
    }

}
